<template>
  <div class="top-nav">
    <div class="nav-left">
      <div class="logo">
        <img src="/ragflow-logo.svg" alt="RAGFlow" />
        <span>电力知识库</span>
      </div>
      <el-menu
        mode="horizontal"
        :default-active="activeMenu"
        class="top-menu"
        :ellipsis="false"
        @select="handleMenuSelect"
      >
        <el-menu-item index="knowledge">
          <el-icon><Notebook /></el-icon>
          知识库
        </el-menu-item>
        <!-- <el-menu-item index="chat">
          <el-icon><ChatDotRound /></el-icon>
          聊天
        </el-menu-item> -->
        <el-menu-item index="search">
          <el-icon><Search /></el-icon>
          搜索
        </el-menu-item>
        <!-- <el-menu-item index="agent">
          <el-icon><Connection /></el-icon>
          Agent
        </el-menu-item> -->
        <el-menu-item index="files">
          <el-icon><Document /></el-icon>
          文件管理
        </el-menu-item>
      </el-menu>
    </div>
    <div class="nav-right">
      <el-dropdown>
        <span class="language-dropdown">
          简体中文
          <el-icon><ArrowDown /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>简体中文</el-dropdown-item>
            <el-dropdown-item>English</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <el-button circle>
        <el-icon><Bell /></el-icon>
      </el-button>
      <el-button circle>
        <el-icon><QuestionFilled /></el-icon>
      </el-button>
      <el-switch
        v-model="isDark"
        inline-prompt
        :active-icon="Moon"
        :inactive-icon="Sunny"
      />
      <el-dropdown>
        <el-avatar :size="32">{{ username.charAt(0) }}</el-avatar>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>个人设置</el-dropdown-item>
            <el-dropdown-item divided>退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { 
  Search, 
  Document,
  Notebook,
  ChatDotRound,
  Connection,
  Bell,
  QuestionFilled,
  Moon,
  Sunny,
  ArrowDown
} from '@element-plus/icons-vue'

const props = defineProps({
  username: {
    type: String,
    required: true
  }
})

const emit = defineEmits(['update:isDark'])

const router = useRouter()
const route = useRoute()

// 根据路由路径获取激活的菜单
const getActiveMenu = (path) => {
  if (path.startsWith('/knowledge')) {
    return 'knowledge'
  }
  // 如果路径以 /files 开头，返回 files
  if (path.startsWith('/files')) {
    return 'files'
  }
  return path.replace('/', '') || 'knowledge'
}

// Initialize activeMenu with current route path
const activeMenu = ref(getActiveMenu(route.path))

// Watch route changes to update activeMenu
watch(() => route.path, (newPath) => {
  activeMenu.value = getActiveMenu(newPath)
})

const isDark = ref(false)

const handleMenuSelect = (index) => {
  router.push(`/${index}`)
}

// Watch isDark changes and emit events
watch(isDark, (newValue) => {
  emit('update:isDark', newValue)
})
</script>

<style scoped lang="scss">
.top-nav {
  padding: 0 16px;
  height: 56px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--el-border-color-light);
  background-color: var(--el-bg-color);

  .nav-left {
    display: flex;
    align-items: center;
    gap: 40px;

    .logo {
      display: flex;
      align-items: center;
      gap: 8px;
      font-size: 18px;
      font-weight: 600;

      img {
        height: 24px;
      }
    }

    .top-menu {
      border-bottom: none;

      :deep(.el-menu-item) {
        display: flex;
        align-items: center;
        gap: 4px;
        height: 56px;
        padding: 0 16px;
        font-size: 14px;

        &.is-active {
          background-color: var(--el-color-primary-light-9);
          color: var(--el-color-primary);
          font-weight: 500;
        }

        &:hover {
          background-color: var(--el-color-primary-light-9);
        }
      }
    }
  }

  .nav-right {
    display: flex;
    align-items: center;
    gap: 16px;

    .language-dropdown {
      display: flex;
      align-items: center;
      gap: 4px;
      cursor: pointer;
      font-size: 14px;
    }
  }
}
</style> 